package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.PersonalReportBean;
import com.imageim.yimirror.common.util.StyleUtil;

import java.util.List;

/**
 * Created by Yuaihen on 2018/3/3.
 */

public class PersonalReportGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private PersonalReportBean.ListBean mData;
    private int itemSize;
    private String[] mWomanItemTitle = {"衬衫领型", "大衣领型", "外套领型", "鞋子款型", "裤子款型", "礼服领型", "面料使用", "旗袍领型", "裙子款型", "图案使用", "外套领型"};
    private String[] mManItemTitle = {"衬衫领型", "大衣领型", "外套领型", "鞋子款型"};
    private List<PersonalReportBean.ListBean.ShirtsBean> mShirts;
    private List<PersonalReportBean.ListBean.OvercoatBean> mOvercoat;
    private List<PersonalReportBean.ListBean.SuitJacketBean> mSuitJacket;
    private List<PersonalReportBean.ListBean.ShoesBean> mShoes;

    public void setData(PersonalReportBean.ListBean data, int size) {
        mData = data;
        itemSize = size;
        notifyDataSetChanged();
    }


    PersonalReportGridViewAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : itemSize;
    }

    @Override
    public Object getItem(int position) {
        return mData == null ? 0 : itemSize;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.personal_gridview_item, parent, false);
            viewHolder.initId(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置数据
        if (mData != null) {
            setItemData(position, viewHolder);
        }

        return convertView;
    }

    private void setItemData(int position, ViewHolder viewHolder) {
        if (itemSize == 4) {
            viewHolder.tvClothingType.setText(mManItemTitle[position]);
        } else {
            viewHolder.tvClothingType.setText(mWomanItemTitle[position]);
        }
        switch (position) {
            case 0:
                if (itemSize == 4) {
                    mShirts = mData.getShirtsBoy();
                } else {
                    mShirts = mData.getShirts();
                }
                if (mShirts == null) {
                    break;
                }
                for (int i = 0; i < mShirts.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getShirtsIconForId(mShirts.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(mShirts.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 1:
                if (itemSize == 4) {
                    mOvercoat = mData.getOvercoatBoy();
                } else {
                    mOvercoat = mData.getOvercoat();
                }
                if (mOvercoat == null) {
                    break;
                }
                for (int i = 0; i < mOvercoat.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getOverCoatIconForId(mOvercoat.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(mOvercoat.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 2:
                if (itemSize == 4) {
                    mSuitJacket = mData.getSuitJacketBoy();
                } else {
                    mSuitJacket = mData.getSuitJacket();
                }
                if (mSuitJacket == null) {
                    break;
                }
                for (int i = 0; i < mSuitJacket.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getSuitJacketIconForId(mSuitJacket.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(mSuitJacket.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 3:
                if (itemSize == 4) {
                    mShoes = mData.getShoesBoy();
                } else {
                    mShoes = mData.getShoes();
                }
                if (mShoes == null) {
                    break;
                }
                for (int i = 0; i < mShoes.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getShoesIconForId(mShoes.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(mShoes.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 4:
                List<PersonalReportBean.ListBean.PantsBean> pants = mData.getPants();
                if (pants == null) {
                    break;
                }
                for (int i = 0; i < pants.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getPantsIconForId(pants.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(pants.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 5:
                List<PersonalReportBean.ListBean.FullDressBean> fullDress = mData.getFullDress();
                if (fullDress == null) {
                    break;
                }
                for (int i = 0; i < fullDress.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getFullDressIconForId(fullDress.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(fullDress.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 6:
                List<PersonalReportBean.ListBean.FabricBean> fabric = mData.getFabric();
                if (fabric == null) {
                    break;
                }
                for (int i = 0; i < fabric.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getFabricIconForId(fabric.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(fabric.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 7:
                List<PersonalReportBean.ListBean.CheongsamBean> cheongsam = mData.getCheongsam();
                if (cheongsam == null) {
                    break;
                }
                for (int i = 0; i < cheongsam.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getCheongSamIconForId(cheongsam.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(cheongsam.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 8:
                List<PersonalReportBean.ListBean.SkirtBean> skirt = mData.getSkirt();
                if (skirt == null) {
                    break;
                }
                for (int i = 0; i < skirt.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getSkirtIconForId(skirt.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(skirt.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            case 9:
                List<PersonalReportBean.ListBean.PatternBean> pattern = mData.getPattern();
                if (pattern == null) {
                    break;
                }
                for (int i = 0; i < pattern.size(); i++) {
                    if (i >= viewHolder.mClothingTypePicArray.length) {
                        return;
                    }
                    viewHolder.mClothingTypePicArray[i].setBackgroundResource(StyleUtil.getPatternIconForId(pattern.get(i).getClothingstypeId()));
                    viewHolder.mSuitTypeArray[i].setBackgroundResource(StyleUtil.getSuitTypeIconForId(pattern.get(i).getSuitType()));
                    viewHolder.mClothingTypePicArray[i].setVisibility(View.VISIBLE);
                    viewHolder.mSuitTypeArray[i].setVisibility(View.VISIBLE);
                }
                break;
            default:

        }
    }


    class ViewHolder {
        private TextView tvClothingType;
        private ImageView ivPic1, ivPic2, ivPic3, ivPic4, ivPic5;
        private ImageView ivSuitType1, ivSuitType2, ivSuitType3, ivSuitType4, ivSuitType5;
        private ImageView[] mClothingTypePicArray, mSuitTypeArray;

        private void initId(View view) {
            tvClothingType = view.findViewById(R.id.tv_personal_gridview_clothing_type);
            View includeView1 = view.findViewById(R.id.include_gridview_clothing_type1);
            View includeView2 = view.findViewById(R.id.include_gridview_clothing_type2);
            View includeView3 = view.findViewById(R.id.include_gridview_clothing_type3);
            View includeView4 = view.findViewById(R.id.include_gridview_clothing_type4);
            View includeView5 = view.findViewById(R.id.include_gridview_clothing_type5);
            ivPic1 = includeView1.findViewById(R.id.iv_personal_gridview_item_clothing_type);
            ivPic2 = includeView2.findViewById(R.id.iv_personal_gridview_item_clothing_type);
            ivPic3 = includeView3.findViewById(R.id.iv_personal_gridview_item_clothing_type);
            ivPic4 = includeView4.findViewById(R.id.iv_personal_gridview_item_clothing_type);
            ivPic5 = includeView5.findViewById(R.id.iv_personal_gridview_item_clothing_type);
            ivSuitType1 = includeView1.findViewById(R.id.iv_personal_gridview_item_suit_type);
            ivSuitType2 = includeView2.findViewById(R.id.iv_personal_gridview_item_suit_type);
            ivSuitType3 = includeView3.findViewById(R.id.iv_personal_gridview_item_suit_type);
            ivSuitType4 = includeView4.findViewById(R.id.iv_personal_gridview_item_suit_type);
            ivSuitType5 = includeView5.findViewById(R.id.iv_personal_gridview_item_suit_type);

            mClothingTypePicArray = new ImageView[]{ivPic1, ivPic2, ivPic3, ivPic4, ivPic5};
            mSuitTypeArray = new ImageView[]{ivSuitType1, ivSuitType2, ivSuitType3, ivSuitType4, ivSuitType5};
        }

    }


}